<?php

namespace app\api\controller\xykeep;

use app\api\model\xykeep\Category as CategoryModel;
use app\common\controller\Api;
use app\api\model\xykeep\Config;
use app\common\model\Area;
use think\Cache;
use think\Config as thinkConfig;

/**
 * 通用接口
 */
class Common extends Api
{
	protected $noNeedLogin = ['init','cityData','categories'];
	protected $noNeedRight = ['*'];


	/**
	 * 加载目标信息和通用配置
	 */
	public function init()
	{


        $customConfig['balance_on'] = thinkConfig::get('site.balance_on');

        $lang['status_2'] = __('Goods Status -2');
        $lang['status_1'] = __('Goods Status -1');
        $lang['status0'] = __('Goods Status 0');
        $lang['status1'] = __('Goods Status 1');
        $lang['status2'] = __('Goods Status 2');
        $lang['status5'] = __('Goods Status 5');

        $appConfig = Config::getValueByName('xykeep');

		$this->success('init', [
			"appStyle" => Config::getValueByName('appstyle'),
            "appConfig" => $appConfig,
			"shareConfig" => Config::getValueByName('share'),
			"tabBarList"  => Config::getValueByName('tabbar'),
            "customConfig" => $customConfig,
            "lang" => $lang
		]);
	}

	/**
     * 根据经纬度获取城市数据
     *
     * @param string $lng     经度
     * @param string $lat     纬度
     */
    public function cityData()
    {
		$lng = $this->request->request('lng');
		$lat = $this->request->request('lat');

		// 添加缓存标记，避免重复执行
		$level = 3;
        if (Cache::tag('geo')->get('geo:area_level_' . $level) !== true) {
            self::area2RedisGeo($level);
        }
		$this->success('城市信息', Area::getCityFromLngLat($lng, $lat));
    }

	/**
     * 区域经纬度到redis
    */
    private function area2RedisGeo($level) {
        $areas = Area::where(['level' => $level])->select();
        $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
        $redis = Cache::store('redis')->handler();
        foreach ($areas as $area) {
            if (method_exists($redis, 'geoadd')) {
                $redis->geoadd($namearr[$area['level']], $area['lng'], $area['lat'], $area['id']);
            }
        }
        Cache::tag('geo')->set('geo:area_level_' . $level, true);
    }

	/**
	 * 省市区
	 */
	public function area()
    {
        $data['provinceData'] = Area::where('level', 1)->order('id asc')->field('id as value, name as label, pid, level')->select();
        foreach ($data['provinceData'] as $k => $p) {
            $data['cityData'][$k] = Area::where(['level' => 2, 'pid' => $p->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            foreach ($data['cityData'][$k] as $i => $c) {
                $data['areaData'][$k][$i] = Area::where(['level' => 3, 'pid' => $c->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
            }
        }

        $this->success('省市区', $data);

    }

    /**
     * 模板分类
     */
    public function categories(){
        $params = $this->request->get();
        $data = CategoryModel::getAllCategory($params);
        $this->success('模板分类', $data);
    }

}
